/*
 
Enterprise Social Network (ESN) is an open-source Java EE framework for
a social network application.

Copyright (C) 2006-07 (see CONTRIBUTORS file in project root)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

package esn.core.util;

import java.security.*;

/**
 * <p>Helper class for creating message digests.</p>
 * 
 * @author Anirban Basu
 *
 */
public abstract class MessageDigestHelper {
	/**
	 * Digests a string and outputs the HEX encoded SHA-1
	 * digest value as a string.
	 * 
	 * @param input string to digest
	 * @return HEX encoded SHA-1 hash of input string
	 * @throws Exception
	 */
	public static String generateHexSHA1Digest(String input) {
		try {
			MessageDigest sha = MessageDigest.getInstance("SHA-1");
			//update initial digest
			sha.update(input.getBytes());
			//output digested message
			byte[] result = sha.digest();
			//HEX encode
			StringBuffer strResult = new StringBuffer();
			for(int i=0; i<result.length; i++) {
				strResult.append(Integer.toHexString((int)(result[i] & 0xff)).trim());
			}
			return strResult.toString();
		}
		catch(NoSuchAlgorithmException nsae) {
			//This is severe! SHA-1 not implemented!!??
			nsae.printStackTrace(); //for administrative reasons
			return null;
		}
	}
}
